//
//  RWGiftCardView.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/30.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWGiftCardView.h"
#import "FTYInfoVertifyTools.h"

@implementation RWGiftCardView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self setGiftCardUI];
    }
    return self;
}

- (void)setGiftCardUI{

    WeakSelf(ws);
    UILabel *reminderIcon = [[UILabel alloc] init];
    [self addSubview:reminderIcon];
    reminderIcon.text = @"请输入礼卡金正面的12位激活码";
    reminderIcon.textColor = [FTYColor grayColor];
    reminderIcon.font = FTY_FONTTSIZE(15);
    [reminderIcon sizeToFit];
    [reminderIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20.*FTY_HEIGHT_BASE);
        make.centerX.mas_equalTo(ws.mas_centerX);
    }];
    
    //三条横线
    UIView *line1 = [[UIView alloc] init];
    [self addSubview:line1];
    line1.backgroundColor = [FTYColor grayColor];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(reminderIcon.mas_bottom).offset(20.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    
    UIView *line2 = [[UIView alloc] init];
    [self addSubview:line2];
    line2.backgroundColor = [FTYColor grayColor];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom).offset(50.*FTY_HEIGHT_BASE);
        make.left.right.height.mas_equalTo(line1);
    }];
    
    UIView *line3 = [[UIView alloc] init];
    [self addSubview:line3];
    line3.backgroundColor = [FTYColor grayColor];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line2.mas_bottom).offset(50.*FTY_HEIGHT_BASE);
        make.left.right.height.mas_equalTo(line1);
    }];
    
    //两条竖线
    UIView *line4 = [[UIView alloc] init];
    [self addSubview:line4];
    line4.backgroundColor = [FTYColor grayColor];
    [line4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(line1);
        make.width.mas_equalTo(.5*FTY_HEIGHT_BASE);
        make.height.mas_equalTo(100.*FTY_HEIGHT_BASE);
    }];
    
    UIView *line5 = [[UIView alloc] init];
    [self addSubview:line5];
    line5.backgroundColor = [FTYColor grayColor];
    [line5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(line1);
        make.width.mas_equalTo(.5*FTY_HEIGHT_BASE);
        make.height.mas_equalTo(line4);
    }];
    
    UITextField *keyTF = [[UITextField alloc] init];
    [self addSubview:keyTF];
    keyTF.placeholder = @"请输入12位激活码";
    keyTF.textColor = [UIColor grayColor];
    keyTF.font = FTY_FONTTSIZE(15);
    [keyTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(line4.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_equalTo(line5.mas_left).offset(-10.*FTY_WIDTH_BASE);
    }];
    _keyTF = keyTF;
    
    UITextField *codeTF = [[UITextField alloc] init];
    [self addSubview:codeTF];
    codeTF.placeholder = @"请输入验证码";
    codeTF.textColor = [UIColor grayColor];
    codeTF.font = FTY_FONTTSIZE(15);
    [codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line2.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(keyTF);
        make.right.mas_equalTo(line5.mas_left).offset(-100.*FTY_WIDTH_BASE);
    }];
    _codeTF = codeTF;
    
    UIButton *getCodeBtn = [[UIButton alloc] init];
    [self addSubview:getCodeBtn];
    getCodeBtn.backgroundColor = [FTYColor getMainColorWithAlpha:1.];
    [getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getCodeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    getCodeBtn.titleLabel.font = FTY_FONTTSIZE(13);
    getCodeBtn.layer.masksToBounds = YES;
    getCodeBtn.layer.cornerRadius = 15.*FTY_WIDTH_BASE;
    [getCodeBtn addTarget:self action:@selector(getCodeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line2.mas_bottom).offset(10.*FTY_HEIGHT_BASE);
        make.right.mas_equalTo(line5.mas_right).offset(-10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(90*FTY_WIDTH_BASE);
        make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
    }];
    
    UIButton *activeBtn = [[UIButton alloc] init];
    [self addSubview:activeBtn];
    activeBtn.backgroundColor = [FTYColor getMainColorWithAlpha:1.];
    [activeBtn setTitle:@"立即激活" forState:UIControlStateNormal];
    [activeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    activeBtn.titleLabel.font = FTY_FONTTSIZE(18);
    activeBtn.layer.masksToBounds = YES;
    activeBtn.layer.cornerRadius = 15.*FTY_WIDTH_BASE;
    [activeBtn addTarget:self action:@selector(activeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [activeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line3.mas_bottom).offset(40.*FTY_HEIGHT_BASE);
        make.left.right.mas_equalTo(line3);
        make.height.mas_equalTo(40.*FTY_HEIGHT_BASE);
    }];
}

#pragma mark - Private Invoke Methods

- (void)getCodeBtnAction:(UIButton *)btn{

    NSString *activeNum = _keyTF.text;
    if (![FTYInfoVertifyTools isnilVertify:activeNum]) {
        
        [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=verify&type=login&member_mobile=%@", [FTYUser sharedUser].userPhoneNumber] parameters:nil block:^(id result, NSError *error) {
            
            if (!error) {
                
                NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
                NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
                if (0 == code) {
                    [[FTYToastView alloc] addTitle:@"验证码已发送～" addView:self];
                } else {
                    [[FTYToastView alloc] addTitle:info addView:self];
                }
            } else {
            
            }
        }];
    } else {
    
        [[FTYToastView alloc] addTitle:@"请输入验证码" addView:self];
    }
}

- (void)activeBtnAction:(UIButton *)btn{
    
    if (_delegate && [_delegate respondsToSelector:@selector(cardActive:)]) {
        [_delegate cardActive:btn];
    }
}

@end
